#light
open System.IO
open System.Text

let main() =
    let args = System.Environment.GetCommandLineArgs()
    if args.Length <= 2 then
        let base = Path.GetFileName(args.[0])
        eprintfn "Usage: %s dir pattern" base
        exit 1
    let directory = args.[1]
    let pattern = args.[2]
    
    for fileName in Directory.GetFiles(directory, pattern) do
        // Open a file stream for the file name
        use inputReader = File.OpenText(fileName)
        
        // Create a lex buffer for use with the generated lexer.
        // The lex buffer reads the inputReader stream.
        let lexBuffer = Lexing.from_text_reader Encoding.ASCII inputReader
        
        // Open an output channel
        let outputFile = Path.ChangeExtension(fileName,"html")
        use outputWriter = File.CreateText(outputFile) :> TextWriter
        
        // Write the header
        fprintfn outputWriter "<html>\n<head></head>\n<pre>"
        
        // Run the generated lexer
        Text2htmllex.convertHtml outputWriter lexBuffer
        
        // Write the footer
        fprintfn outputWriter "</pre>\n</html>\n"

do main()
